﻿@using SubstrateHelper.Models;
@using SubstrateHelper.Data;
@using ProcessBase.Models;
@using System.Collections.Concurrent;
@using System.Collections.Generic;
@using System.Linq;
@using System.Timers;
@using BootstrapBlazor.Components;


@inject SubstrateHelperDbContext Db
@inject MessageService MessageService

@inject NavigationManager NavigationManager
<div>
    <div class="m-1">
        <span>
            <strong class="text-success">主批号 </strong>（包含）：
            <input type="text" @bind="LotNo" style="width:100px">
        </span>
        @*<span class="ml-5">
            <strong class="text-success">来料标识 </strong>（包含）：
            <input type="text" @bind="InputLotNo" style="width:100px">
        </span>*@
        <span class="ml-5">
            <strong class="text-success">产出标识 </strong>（包含）：
            <input type="text" @bind="OutputLotNo" style="width:100px">
        </span>

        <span class="ml-5">
            <strong class="text-success">工序 </strong>：
            <select @bind="SelectedProcess">
                <option value="-1">所有类型</option>
                @if (Processes != null)
                {
                    @for (int i = 0; i < Processes.Count; i++)
                    {
                        <option value="@i">@Processes[i]</option>
                    }
                }

            </select>
        </span>


        @*<span class="ml-5">
            <strong class="text-success">选择</strong>：
            <select @bind="SelectedLotToShowDetail">
                @if (LotsToShowDetail != null)
                {
                    @foreach (var a in LotsToShowDetail)
                    {
                        <option value="@a.Key">@a.Value</option>
                    }
                }
            </select>
        </span>*@






        <Button class="ml-5" IsOutline="false" Color="Color.Success" @onclick="DoQuery">查  询</Button>

        <Button class="ml-5" IsOutline="false" Color="Color.Primary" @onclick="SaveRecord">保存更改</Button>

    </div>

    

    <hr class="bg-primary" style="height:5px;" />

    <ComponentProcessDetail ProcessDetails="ProcessDetails" @ref="componentProcessDetail"/>


</div>
<Message />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
@code {

    ComponentProcessDetail componentProcessDetail { get; set; }



    [Parameter]
    public string Guid
    {
        get
        {
            return _Guid;
        }
        set
        {
            _Guid = value;
            _UpdateDetail(_Guid);
        }
    }
    string _Guid;

    List<string> Processes
    {
        get
        {
            if (_Processes == null)
            {
                try
                {
                    _Processes = Db.Parameters.Select(x => x.ProcessName).Distinct().OrderBy(x => x).ToList();
                }
                catch
                {
                    _Processes = null;
                }
            }
            return _Processes;
        }


    }
    List<string> _Processes = null;



    List<SelectedItem> QualityStates
    {
        get
        {
            if (_QualityStates == null)
            {
                _QualityStates = new List<SelectedItem>();
                _QualityStates.Add(new SelectedItem(null, "未处理"));

                foreach (var x in Db.CandidateLists.Where(x => x.Category == "质量状态").Select(x => x.Candidate))
                {
                    var item = new SelectedItem(x, x);
                    _QualityStates.Add(item);
                }
            }
            return _QualityStates;
        }
        set
        {
            _QualityStates = value;
        }
    }
    List<SelectedItem> _QualityStates = null;

    List<SelectedItem> InputLots { get; set; }

    int SelectedProcess
    {
        get
        {
            return _SelectedProcess;
        }
        set
        {
            _SelectedProcess = value;
        }
    }
    int _SelectedProcess = -1;



    string LotNo { get; set; }
    string OutputLotNo { get; set; }
    string InputLotNo { get; set; }

    List<ProcessBase.Models.ProcessDetail> ProcessDetails
    {
        get
        {
            return _ProcessDetails;
        }
        set
        {
            _ProcessDetails = value;

        }
    }
    List<ProcessBase.Models.ProcessDetail> _ProcessDetails = null;

    List<KeyValuePair<string, string>> LotsToShowDetail { get; set; }
    string SelectedLotToShowDetail
    {
        get
        {
            return _SelectedLotToShowDetail;
        }
        set
        {
            _SelectedLotToShowDetail = value;

            Guid = _SelectedLotToShowDetail;

        }
    }
    string _SelectedLotToShowDetail = null;

    private void DoQuery()
    {
        try
        {
            //清空
            Guid = null;




            //主批号
            IQueryable<ProcessBase.Models.Record> query = Db.Records;
            if (!string.IsNullOrWhiteSpace(LotNo))
            {
                query = query.Where(x => (!string.IsNullOrWhiteSpace(x.MainLot))&& x.MainLot.Contains(LotNo));
            }
            

            

            //产出标识
            
            if (!string.IsNullOrWhiteSpace(OutputLotNo))
            {
                query = query.Where(x =>(!string.IsNullOrWhiteSpace(x.OutLot))&& x.OutLot.Contains(OutputLotNo));                
            }

            //工序
            
            if (SelectedProcess >= 0)
            {
                query = query.Where(x => x.ProcessName == Processes[SelectedProcess]);   
            }

            //
            var guids=query.Select(x=>x.Guid).Distinct().ToList();

            if (guids.Count > 1)
            {
                MessageService?.Show(new MessageOption()
                {
                    Content = $"返回{guids.Count}个结果。只显示第一个结果。\n请输入更多信息过滤得到所需结果。",
                    Icon = "fa fa-info-circle",
                    ShowDismiss = true,
                    Color = Color.Success
                });
                
            }
            else if (guids.Count <= 0)
            {
                MessageService?.Show(new MessageOption()
                {
                    Content = $"返回0个结果。",
                    Icon = "fa fa-info-circle",
                    ShowDismiss = true,
                    Color = Color.Success
                });
                return;
            }
            //

            Guid = guids[0];







        }
        catch (Exception ex)
        {
            MessageService?.Show(new MessageOption()
            {
                Content = $"发生错误：{ex.Message}",
                Icon = "fa fa-info-circle",
                ShowDismiss = true,
                Color = Color.Danger
            });
        }
    }

    private void _UpdateDetail(string guid)
    {




        //       
        ProcessDetails = Helpers.ProcessDetailHelper.GetDetails(guid);

        //



    }







    private void SaveRecord()
    {
        componentProcessDetail.SaveRecord();


    }










}

